package com.sqlcheck.util;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

@Slf4j
public class XmlScanDebugUtil {

    /**
     * 调试XML文件检测过程
     */
    public static void debugXmlFileDetection(Path xmlFilePath) {
        try {
            if (!Files.exists(xmlFilePath)) {
                log.warn("文件不存在: {}", xmlFilePath);
                return;
            }

            if (!xmlFilePath.toString().toLowerCase().endsWith(".xml")) {
                log.warn("文件不是XML格式: {}", xmlFilePath);
                return;
            }

            log.info("=== 开始调试XML文件检测: {} ===", xmlFilePath);

            // 读取文件前10行
            List<String> lines = Files.readAllLines(xmlFilePath);
            String content = String.join("\n", lines.subList(0, Math.min(10, lines.size())));

            log.info("文件前10行内容:");
            log.info("{}", content);

            // 检查各种MyBatis标识
            boolean hasMybatis = content.contains("mybatis");
            boolean hasMapper = content.contains("mapper");
            boolean hasDoctypeMapper = content.contains("<!DOCTYPE mapper");
            boolean hasMybatisDtd = content.contains("http://mybatis.org/dtd/");
            boolean hasSelect = content.contains("<select");
            boolean hasInsert = content.contains("<insert");
            boolean hasUpdate = content.contains("<update");
            boolean hasDelete = content.contains("<delete");
            boolean hasSql = content.contains("<sql");
            boolean hasResultMap = content.contains("<resultMap");
            boolean hasParameterMap = content.contains("<parameterMap");

            log.info("MyBatis标识检测结果:");
            log.info("  mybatis: {}", hasMybatis);
            log.info("  mapper: {}", hasMapper);
            log.info("  <!DOCTYPE mapper: {}", hasDoctypeMapper);
            log.info("  http://mybatis.org/dtd/: {}", hasMybatisDtd);
            log.info("  <select: {}", hasSelect);
            log.info("  <insert: {}", hasInsert);
            log.info("  <update: {}", hasUpdate);
            log.info("  <delete: {}", hasDelete);
            log.info("  <sql: {}", hasSql);
            log.info("  <resultMap: {}", hasResultMap);
            log.info("  <parameterMap: {}", hasParameterMap);

            // 判断是否为MyBatis XML
            boolean isMybatis = hasMybatis || hasMapper || hasDoctypeMapper || hasMybatisDtd ||
                    hasSelect || hasInsert || hasUpdate || hasDelete || hasSql || hasResultMap || hasParameterMap;

            log.info("最终判断结果: 是否为MyBatis XML = {}", isMybatis);
            log.info("=== XML文件检测调试完成 ===");

        } catch (IOException e) {
            log.error("调试XML文件检测失败: {}", xmlFilePath, e);
        }
    }

    /**
     * 扫描目录并调试所有XML文件
     */
    public static void debugDirectoryXmlFiles(Path directoryPath) {
        try {
            if (!Files.exists(directoryPath) || !Files.isDirectory(directoryPath)) {
                log.warn("目录不存在或不是目录: {}", directoryPath);
                return;
            }

            log.info("=== 开始扫描目录XML文件: {} ===", directoryPath);

            Files.walk(directoryPath)
                    .filter(Files::isRegularFile)
                    .filter(path -> path.toString().toLowerCase().endsWith(".xml"))
                    .forEach(xmlPath -> {
                        log.info("发现XML文件: {}", xmlPath);
                        debugXmlFileDetection(xmlPath);
                        log.info("---");
                    });

            log.info("=== 目录XML文件扫描完成 ===");

        } catch (IOException e) {
            log.error("扫描目录XML文件失败: {}", directoryPath, e);
        }
    }

    /**
     * 统计目录文件类型
     */
    public static void countFileTypes(Path directoryPath) {
        try {
            if (!Files.exists(directoryPath) || !Files.isDirectory(directoryPath)) {
                log.warn("目录不存在或不是目录: {}", directoryPath);
                return;
            }

            log.info("=== 开始统计目录文件类型: {} ===", directoryPath);

            long totalFiles = Files.walk(directoryPath)
                    .filter(Files::isRegularFile)
                    .count();

            long xmlFiles = Files.walk(directoryPath)
                    .filter(Files::isRegularFile)
                    .filter(path -> path.toString().toLowerCase().endsWith(".xml"))
                    .count();

            long javaFiles = Files.walk(directoryPath)
                    .filter(Files::isRegularFile)
                    .filter(path -> path.toString().toLowerCase().endsWith(".java"))
                    .count();

            long sqlFiles = Files.walk(directoryPath)
                    .filter(Files::isRegularFile)
                    .filter(path -> path.toString().toLowerCase().endsWith(".sql"))
                    .count();

            log.info("文件类型统计:");
            log.info("  总文件数: {}", totalFiles);
            log.info("  XML文件数: {}", xmlFiles);
            log.info("  Java文件数: {}", javaFiles);
            log.info("  SQL文件数: {}", sqlFiles);
            log.info("  其他文件数: {}", totalFiles - xmlFiles - javaFiles - sqlFiles);

            log.info("=== 文件类型统计完成 ===");

        } catch (IOException e) {
            log.error("统计目录文件类型失败: {}", directoryPath, e);
        }
    }
}
